//
//  CPABOUTController.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/4.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPABOUTController.h"
#import <SafariServices/SafariServices.h>

@interface CPABOUTController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UITableView *abouTab;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation CPABOUTController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.RImage = @"share";
    self.dataSource = [NSMutableArray arrayWithArray:@[@"用户使用协议",@"隐私协议",@"使用说明"]];
    CPABTHeadView *header = [[CPABTHeadView alloc] initWithFrame:CGRectMake(0, 0, kW, kW*0.45)];
    UITableView *abouTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kW, kH) style:(UITableViewStyleGrouped)];
    [abouTab registerClass:[UITableViewCell class] forCellReuseIdentifier:@"aboutCell"];
    abouTab.delegate = self;
    abouTab.dataSource = self;
    abouTab.showsVerticalScrollIndicator = NO;
    abouTab.tableHeaderView = header;
    [self.view addSubview:abouTab];
    self.abouTab = abouTab;
    [abouTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.mas_equalTo(self.view.mas_top);
            make.bottom.mas_equalTo(self.view.mas_bottom);
        }
    }];
}

#pragma mark --
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"aboutCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];}
    cell.textLabel.text = _dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *url = nil;
    if (indexPath.row == 0) {
        url = @"http://www.restcy.com/2018/07/14/appanzhuangxukeshiyongxieyi/";
    }else if (indexPath.row == 1){
        url = @"http://www.restcy.com/2018/07/yinsizhengce/";
    }else{
        url = @"http://www.restcy.com/2019/02/27/shiyongshuoming/";
    }
    SFSafariViewController *sVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:url] entersReaderIfAvailable:YES];
    [self presentViewController:sVC animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64.0;
}

- (void)cp_rightBtn
{
    NSString *sketchpad = @"https://itunes.apple.com/cn/app/Sketchpad-Pro/1455774052?mt=8";
    [self cp_share:sketchpad image:nil handle:^(UIActivityType  _Nullable activityType, BOOL completed) {}];
}

@end
#pragma mark - **************** CPABTHeadView
@implementation CPABTHeadView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *lg = [[UIImageView alloc] initWithFrame:CGRectZero];
        lg.layer.cornerRadius = 16;
        lg.layer.masksToBounds = YES;
        lg.image = [UIImage imageNamed:@"defaulticon"];
        [self addSubview:lg];
        self.lg = lg;
        
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectZero];
        name.text = @"中国成语大全";
        name.textAlignment = 1;
        name.font = [UIFont systemFontOfSize:16.0];
        [self addSubview:name];
        self.name = name;
        
        UILabel *ver = [[UILabel alloc] initWithFrame:CGRectZero];
        ver.text = [NSString stringWithFormat:@"版本：%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
        ver.textAlignment = 1;
        ver.textColor = JMColor(170, 170, 170);
        ver.font = [UIFont systemFontOfSize:11.0];
        [self addSubview:ver];
        self.ver = ver;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat h = (self.height-70-self.height*0.17)/2;
    _lg.frame = CGRectMake(self.width/2-35, self.height*0.17, 70, 70);
    _name.frame = CGRectMake(0, CGRectGetMaxY(_lg.frame), self.width, h);
    _ver.frame = CGRectMake(0, CGRectGetMaxY(_name.frame)-10, self.width, h);
}
@end
